package cn.oowoo.rabbitmq.akc;

import cn.oowoo.rabbitmq.config.RabbitConfig;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ZhangBin
 * @Date 2021/5/7 18:41
 * @Description: 手动确认-生产者
 *
 **/
@Slf4j
public class AckProducer {

    public static void main(String[] args) throws Exception {
        Connection connection = RabbitConfig.createConnection();
        // 创建消息通道
        Channel channel = connection.createChannel();

        // 声明队列（默认交换机AMQP default，Direct）
        channel.queueDeclare(RabbitConfig.QUEUE_ACK_NAME, false, false, false, null);

        // 发送消息
        Map<String, Object> headers = new HashMap<>();
        headers.put("token", "11111111");
        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                .deliveryMode(2)
                .contentEncoding("UTF-8")
                .headers(headers)
                .build();
        String msg = "ACK消息 ";
        for (int i =0; i<5; i++){
            if(i == 3){
                msg = "ACK消息 异常 ";//演示异常数据的情况
            }else if(i == 4){
                msg = "ACK消息 拒收 ";//演示拒收的情况
            }
            //直连模式可以不需要指定交换机
            channel.basicPublish("", RabbitConfig.QUEUE_ACK_NAME, properties, (msg+i).getBytes());
            log.info(msg);
        }
        //消息发送完毕关闭信道，关闭链接
        channel.close();
        connection.close();
    }

}

